package org.ala.tiktools.generator.mybatis.creator.entity;

import org.ala.tiktools.generator.mybatis.config.info.Part;
import org.ala.tiktools.generator.mybatis.dbinfo.TableInfo;
import org.ala.tiktools.generator.mybatis.tools.Tools;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ala
 * @date 2024-12-31 11:30
 */
public class EntityInfo {

    /**
     *  表信息
     */
    protected String tableName, tableDesc;
    /**
     *  类名
     */
    protected String className;
    /**
     *  作者信息
     */
    protected String author, authorEnglish;
    /**
     *  package
     */
    protected String pkg;
    /**
     *  创建时间
     */
    protected String createTime;
    /**
     *  import信息
     */
    protected Set<String> imports = new HashSet<>();
    /**
     *  字段
     */
    protected List<FieldInfo> fields;
    /**
     *  唯一自增主键
     */
    protected FieldInfo onlyAutoIncrementPrimary;
    /**
     *  是否支持swagger
     */
    protected boolean swagger;


    public static EntityInfo build(Part part, TableInfo table) {
        EntityInfo e = new EntityInfo();
        e.pkg = part.getOut().getEntity().getPkg();
        //  如果包含swagger
        if (part.getSwagger().isEnabled()) {
            e.imports.add("io.swagger.annotations.ApiModel");
            e.imports.add("io.swagger.annotations.ApiModelProperty");
            e.imports.add("java.io.Serializable");
        }
        //  当前日期时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        e.swagger = part.getSwagger().isEnabled();
        e.tableName = table.getName();
        e.tableDesc = table.getDesc();
        e.author = part.getAuthor().getName();
        e.authorEnglish = part.getAuthor().getEnglish();
        e.className = Tools.lowToUpFirst(Tools.hump(e.tableName)) + part.getOut().getEntity().getSuffix();
        e.createTime = sdf.format(new Date());
        e.fields = table.getColumns().stream().map(FieldInfo::build).collect(Collectors.toList());
        e.fields.forEach(f -> {
            if (f.isOnlyAutoIncrementPrimaryKey()) {
                e.onlyAutoIncrementPrimary = f;
            }
        });
        return e;
    }


    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getTableDesc() {
        return tableDesc;
    }

    public void setTableDesc(String tableDesc) {
        this.tableDesc = tableDesc;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getPkg() {
        return pkg;
    }

    public void setPkg(String pkg) {
        this.pkg = pkg;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public Set<String> getImports() {
        return imports;
    }

    public void setImports(Set<String> imports) {
        this.imports = imports;
    }

    public List<FieldInfo> getFields() {
        return fields;
    }

    public void setFields(List<FieldInfo> fields) {
        this.fields = fields;
    }

    public boolean isSwagger() {
        return swagger;
    }

    public void setSwagger(boolean swagger) {
        this.swagger = swagger;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getAuthorEnglish() {
        return authorEnglish;
    }

    public void setAuthorEnglish(String authorEnglish) {
        this.authorEnglish = authorEnglish;
    }

    public FieldInfo getOnlyAutoIncrementPrimary() {
        return onlyAutoIncrementPrimary;
    }

    public void setOnlyAutoIncrementPrimary(FieldInfo onlyAutoIncrementPrimary) {
        this.onlyAutoIncrementPrimary = onlyAutoIncrementPrimary;
    }
}
